﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using FluentData;
using Framework.Core;


namespace Framework
{
    /// <summary>
    /// 身份验证服务
    /// </summary>
    public class AuthenticationService : IAuthenticationService
    {
        public bool SignIn(byte[] cardNumberCipher)
        {
            throw new NotImplementedException();
        }

        public bool SignIn(string hotelOrgId, string userName, string password)
        {
            var daEmployee = new EmployeeDataAccess();

            var user = daEmployee.VerifyPassword(userName, password);

            if (user == null)
                return false;

            return true;
        }

        public bool SignIn(string userName, string password)
        {
            var daEmployee = new EmployeeDataAccess();

            var user = daEmployee.VerifyPassword(userName, password);

            if (user == null)
                return false;

            return true;
        }

        public void SignOut()
        {
            throw new NotImplementedException();
        }       

        //public bool AuthorizeControl(string pageName, string controlId, User user, string orgId)
        //{
        //    if (string.IsNullOrEmpty(controlId))
        //        throw new ArgumentNullException("空的父控件名");          
        //    SecurityCache cache = SecurityCache.GetInstance();
        //    IEnumerable<RulePage> list = cache.Control_Rule_SubControl_Cache.Where(m => m.PageName.Equals(pageName,
        //        StringComparison.CurrentCultureIgnoreCase) && m.ControlName.Equals(controlId,StringComparison.CurrentCultureIgnoreCase));
        //    if (list.Count() > 0)
        //    {
        //        //AuthorizeRule(user, list.FirstOrDefault().RuleID, orgId);
        //    }
        //    return false;
        //}

        //private bool AuthorizeRule(User user, string rule, string orgId)
        //{
        //    OrgTree tree = OrgTreeProvider.GetOrganizationTree();
        //    OrgTreeNode orgIdNode = tree.SelectNodeById(orgId);

        //    if (orgIdNode == null)
        //    {
        //        return false;
        //    }
        //    if (string.IsNullOrEmpty(orgIdNode.FullName))
        //        return false;
        //    //从功能获取对应的角色列表
        //    string[] expectRoles = RuleParser.GetParsedExpression(rule);

        //    if (expectRoles == null)
        //        return true;

        //    OrgTreeNode fullNameNode = tree.SelectNode(orgIdNode.FullName);

        //    if (user.Roles != null)
        //    {
        //        foreach (string r in expectRoles)
        //        {
        //            string role = r;
        //            //没有这样的角色
        //            if (!user.Roles.ContainsKey(role))
        //                continue;
        //            List<OrganizationScope> scopes = user.Roles[role];
        //            //检索用户该角色对应范围是否超出目前登录的组织节点范围
        //            foreach (OrganizationScope sc in scopes)
        //            {
        //                OrgTreeNode node = tree.SelectNodeById(sc.OrgId);
        //                if (node >= fullNameNode)
        //                    return true;
        //            }
        //        }
        //    }
        //    ////如果自己无权操作，可以看看是否有别人赋予自己的令牌
        //    //if (user.Credentials != null)
        //    //{
        //    //    var validCredentials = user.Credentials.Where(c => c.ValidEndTime >= DateTime.Now);

        //    //    if (validCredentials.Count() > 0)
        //    //    {
        //    //        return validCredentials.Any(c => AuthorizeRule(c.Roles, rule, orgIdNode.FullName));
        //    //    }

        //    //    return false;
        //    //}
        //    return false;
        //}
    }
}
